package hlhj.fhp.newslib.adapters

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.activitys.SpecialInfoAty
import hlhj.fhp.newslib.javabean.SpecialDetailBean
import hlhj.fhp.newslib.network.Urls
import hlhj.fhp.newslib.utils.GlideUtil
import hlhj.fhp.newslib.utils.NumUtil
import java.util.ArrayList

/**
 * Created by Administrator on 2018\6\27 0027.
 */
class SpecialMultiAdp(datas:ArrayList<SpecialDetailBean.DataBean.ListBean>)
    :BaseMultiItemQuickAdapter<SpecialDetailBean.DataBean.ListBean,BaseViewHolder>(datas) {
    init {
        addItemType(1, R.layout.special_news_detail_item)
        addItemType(4, R.layout.special_news_detail_item)
    }
    override fun convert(helper: BaseViewHolder?, item: SpecialDetailBean.DataBean.ListBean?) {
        when(helper?.itemViewType){
            1->{
                helper?.setVisible(R.id.itemViewNum,true)
                if (item?.extract_img!!.size>0){
                    if (item?.extract_img[0].contains("http")){
                        Glide.with(mContext).load(item?.extract_img[0]).apply(GlideUtil.loadIMGOption())
                                .into(helper?.getView(R.id.itemImg)!!)
                    }else{
                        Glide.with(mContext).load(Urls.Base_URL+"/"+item?.extract_img[0]).apply(GlideUtil.loadIMGOption())
                                .into(helper?.getView(R.id.itemImg)!!)
                    }
                }
                helper?.setText(R.id.itemTittle,item?.title)
//                helper?.setText(R.id.itemFrom,item?.release)
                if (item?.click_rate>=1000){
                    helper?.setText(R.id.itemViewNum,"${NumUtil.formatNum((item?.click_rate/1000f),1)}")
                }else{

                    helper?.setText(R.id.itemViewNum,"${item?.click_rate}")
                }
                helper?.setText(R.id.itemTime,item?.add_time)
                helper?.itemView.setOnClickListener {
                    data[helper?.adapterPosition].click_rate=data[helper?.adapterPosition].click_rate+1
                    notifyDataSetChanged()
                    val intent = Intent(mContext, SpecialInfoAty::class.java)
                    if (item?.extract_img.size>0){
                        intent.putExtra("thumb",item?.extract_img[0])
                    }else{
                        intent.putExtra("thumb","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531470814752&di=49e3232f35fa9c8439160a2d071bf1cb&imgtype=0&src=http%3A%2F%2Fimg382.ph.126.net%2Fp4dMCiiHoUGxf2N0VLspkg%3D%3D%2F37436171903673954.jpg")
                    }
                    intent.putExtra("id",item?.id)
                    mContext.startActivity(intent)
                }
            }
            4->{
                helper?.setVisible(R.id.itemViewNum,false)
                helper?.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.itemTime,"广告")
                helper?.setTextColor(R.id.itemTime, Color.parseColor("#FFB400"))
                if (item!!.advertisement_img[0]!=null){
                    if (item!!.advertisement_img[0].contains("http")){
                        Log.e("fhp","有http")
                        Glide.with(mContext).load(item?.advertisement_img?.get(0))
                                .apply(GlideUtil.loadIMGOption())
                                .into(helper?.getView(R.id.itemImg)!!)
                    }else{
                        Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(0))
                                .apply(GlideUtil.loadIMGOption())
                                .into(helper?.getView(R.id.itemImg)!!)
                    }
                }
                helper?.itemView!!.setOnClickListener {
                    val uri = Uri.parse(item?.advertisement_url)
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    mContext.startActivity(intent)
                }
                /*val loImg = helper?.getView<LinearLayout>(R.id.loimg)
                val img1 = helper?.getView<ImageView>(R.id.img1)
                val img2 = helper?.getView<ImageView>(R.id.img2)
                val img3 = helper?.getView<ImageView>(R.id.img3)
                if (item!!.advertisement_img[0]!=null){
                    if (item!!.advertisement_img[0].contains("http")){
                        Log.e("fhp","有http")
                        Glide.with(mContext).load(item?.advertisement_img?.get(0))
                                .apply(GlideUtil.loadIMGOption())
                                .into(helper?.getView(R.id.img1)!!)
                    }else{
                        Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(0))
                                .apply(GlideUtil.loadIMGOption())
                                .into(helper?.getView(R.id.img1)!!)
                    }

                }
                Log.i("fhh",item?.extract_img.size.toString())
                when(item!!.advertisement_img.size){
                    0->{
                        loImg.visibility= View.GONE
                    }
                    1->{
                        if (item!!.advertisement_img[0]!=null){
                            if (item?.advertisement_img[0].contains("http")){
                                Glide.with(mContext).load(item?.advertisement_img?.get(0))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img1)
                            }else{
                                Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(0))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img1)
                            }

                        }
                        loImg.visibility= View.VISIBLE
                        img1.visibility= View.VISIBLE
                        img2.visibility= View.GONE
                        img3.visibility= View.GONE
                    }
                    2->{
                        if (item!!.advertisement_img[0]!=null){
                            if (item?.advertisement_img[0].contains("http")){
                                Glide.with(mContext).load(item?.advertisement_img?.get(0))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img1)
                            }else{
                                Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(0))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img1)
                            }
                        }
                        if (item!!.advertisement_img[1]!=null) {
                            if (item?.advertisement_img[1].contains("http")){
                                Glide.with(mContext).load(item?.advertisement_img?.get(1))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img2)
                            }else{
                                Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(1))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img2)
                            }

                        }
                        loImg.visibility= View.VISIBLE
                        img1.visibility= View.VISIBLE
                        img2.visibility= View.VISIBLE
                        img3.visibility= View.GONE
                    }
                    else->{
                        if (item!!.advertisement_img[0]!=null){
                            if (item?.advertisement_img[0].contains("http")){
                                Glide.with(mContext).load(item?.advertisement_img?.get(0))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img1)
                            }else{
                                Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(0))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img1)
                            }
                        }
                        if (item!!.advertisement_img[1]!=null) {
                            if (item?.advertisement_img[1].contains("http")){
                                Glide.with(mContext).load(item?.advertisement_img?.get(1))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img2)
                            }else{
                                Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(1))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img2)
                            }

                        }
                        if (item!!.advertisement_img[2]!=null){
                            if (item?.advertisement_img[2].contains("http")){
                                Glide.with(mContext).load(item?.advertisement_img?.get(2))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img3)
                            }else{
                                Glide.with(mContext).load(Urls.Base_URL+"/"+item?.advertisement_img?.get(2))
                                        .apply(GlideUtil.loadIMGOption())
                                        .into(img3)
                            }

                        }

                        loImg.visibility= View.VISIBLE
                        img1.visibility= View.VISIBLE
                        img2.visibility= View.VISIBLE
                        img3.visibility= View.VISIBLE
                    }
                }
                helper.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.tvOwner,item?.release)*/
            }
        }
    }
}